// pages/moneyList/moneyList.js
import {formatTime} from '../../utils/util'
let db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyList:[],
    admin:[],
    isAdmin:false
  },
  confirmWx(e){
    let {index}=e.currentTarget.dataset
    let type=this.data.moneyList[index].type
    let that = this
    wx.showLoading({
      title: '请稍等...'
    })
    let zhongjiang = that.data.moneyList[index].zhongjiang
    zhongjiang[0].isConfirm = true
    db.collection('moneyList')
    .where({
      type
    })
    .update({
      // data 传入需要局部更新的数据
      data: {
        zhongjiang
      }
    })
    .then((e)=>{
      wx.hideLoading({
        success: (res) => {},
      })
      that.setData({
        ['moneyList['+index+'].zhongjiang']:zhongjiang
      })
    })
    .catch((e)=>{
      wx.hideLoading({
        success: (res) => {},
      })
      console.log(e)
    })
    db.collection('admin')
    .where({
      type
    })
    .update({
      // data 传入需要局部更新的数据
      data: {
        zhongjiang
      }
    })
  },
  change(e){
    console.log(e)
    let {index}=e.currentTarget.dataset
    this.setData({
      ['moneyList['+index+'].zhongjiang[0].wx']:e.detail.value
    })
  },
  copyType(e){
    let {content} = e.currentTarget.dataset
    wx.setClipboardData({
      data: content+'',
    })
  },
  admin(){
    wx.setStorageSync('admin', this.data.admin)
    wx.navigateTo({
      url: '/pages/verify/verify',
    })
  },
  getDetailLink(e){
    let {index} = e.currentTarget.dataset
    wx.setStorageSync('getDetail', this.data.moneyList[index])
    wx.navigateTo({
      url: '/pages/getDetail/getDetail',
    })
  },
  watchAll(e){
    let {index} = e.currentTarget.dataset
    wx.setStorageSync('userList', this.data.moneyList[index].user)
    wx.navigateTo({
      url: '/pages/userList/userList',
    })
  },
  isUserLogin(){
    let isUserLogin
    if(wx.getStorageSync('userInfo')){
      isUserLogin=true
    } else {
      isUserLogin=false
    }
    this.setData({isUserLogin,userInfo:wx.getStorageSync('userInfo')})
    wx.hideLoading({
      success: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    getApp().WX_LOGIN(function(){
      that.isUserLogin()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().chaping()
    if(wx.getStorageSync('openId')=='oNcMx5RAmUJdHaVDDAGAo0UANjZI'){
      this.setData({
        isAdmin:true
      })
    }
    let that = this
    db.collection('moneyList')
    .get()
    .then((e)=>{
      console.log(e)
      let openId = wx.getStorageSync('openId')
      e.data.forEach(item=>{
        item.endTime1 = formatTime(item.endTime)
        if(item.user.length <10 ){
          item.userLimit = item.user
        } else {
          item.userLimit = item.user.slice(0,9)
        }
        if(item.zhongjiang.length>0){
          if(item.zhongjiang[0].openId == wx.getStorageSync('openId')){
            item.isGet = true
          }
        }
        item.user.forEach(itemIn=>{
          if(itemIn.openId == openId){
            item.isJoin = true
          }
        })
      })
      that.setData({
        moneyList:e.data.reverse()
      })
    })
    db.collection('admin')
    .get()
    .then((e)=>{
      console.log(e)
      that.setData({
        admin:e.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})